package com.eh.erd.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eh.common.exception.Assert;
import com.eh.common.result.ResponseEnum;
import com.eh.common.util.MD5;
import com.eh.erd.base.util.JwtUtils;
import com.eh.erd.core.mapper.UserAccountMapper;
import com.eh.erd.core.mapper.UserInfoMapper;
import com.eh.erd.core.mapper.UserLoginRecordMapper;
import com.eh.erd.core.pojo.entity.UserAccount;
import com.eh.erd.core.pojo.entity.UserInfo;
import com.eh.erd.core.pojo.entity.UserLoginRecord;
import com.eh.erd.core.pojo.query.UserInfoQuery;
import com.eh.erd.core.pojo.vo.LoginVo;
import com.eh.erd.core.pojo.vo.RegisterVo;
import com.eh.erd.core.pojo.vo.UserInfoVo;
import com.eh.erd.core.service.UserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户基本信息 服务实现类
 * </p>
 *
 * @author zhuyuanwei
 * @since 2021-04-29
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private UserAccountMapper userAccountMapper;

    @Autowired
    private UserLoginRecordMapper userLoginRecordMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void register(RegisterVo registerVo) {
        /**
         * 根据手机号查询是否注册过
         * 如果已经注册返回注册失败，该号码已被注册
         *
         */
        QueryWrapper<UserInfo> queryWrapper=new <UserInfo>QueryWrapper();
        queryWrapper.eq("mobile",registerVo.getMobile());
        Integer integer = baseMapper.selectCount(queryWrapper);
        Assert.isTrue(integer==0, ResponseEnum.MOBILE_EXIST_ERROR);
        UserInfo userInfo=new UserInfo();
        userInfo.setUserType(registerVo.getUserType());
        userInfo.setMobile(registerVo.getMobile());
        userInfo.setNickName(registerVo.getMobile());
        userInfo.setName(registerVo.getMobile());
        userInfo.setPassword(MD5.encrypt(registerVo.getPassword()));
        userInfo.setStatus(UserInfo.STATUS_NORMAL);
        userInfo.setHeadImg(UserInfo.USER_AVAtAR);
        baseMapper.insert(userInfo);


        UserAccount userAccount=new UserAccount();
        userAccount.setUserId(userInfo.getId());
        userAccountMapper.insert(userAccount);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserInfoVo login(LoginVo loginVo, String ip) {
        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_type",loginVo.getUserType()).eq("mobile",loginVo.getMobile());
        UserInfo userInfo = baseMapper.selectOne(queryWrapper);

        Assert.notNull(userInfo,ResponseEnum.LOGIN_MOBILE_ERROR);
        Assert.equals(MD5.encrypt(loginVo.getPassword()),userInfo.getPassword(),ResponseEnum.LOGIN_PASSWORD_ERROR);
        Assert.equals(userInfo.getStatus(),UserInfo.STATUS_NORMAL,ResponseEnum.LOGIN_LOKED_ERROR);

        //记录日志
        UserLoginRecord userLoginRecord=new UserLoginRecord();
        userLoginRecord.setUserId(userInfo.getId());
        userLoginRecord.setIp(ip);
        userLoginRecordMapper.insert(userLoginRecord);

        String token= JwtUtils.createToken(userInfo.getId(),userInfo.getName());

        UserInfoVo userInfoVo=new UserInfoVo();
        BeanUtils.copyProperties(userInfo,userInfoVo);
        userInfoVo.setToken(token);

        return userInfoVo;
    }

    @Override
    public IPage<UserInfo> getUserInfoListByPage(Long page, Long limit, UserInfoQuery userInfoQuery) {
        IPage<UserInfo> pages=new Page(page,limit);
        if(userInfoQuery==null){
            IPage<UserInfo> userInfoIPage = baseMapper.selectPage(pages, null);
            return userInfoIPage;
        }

        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(userInfoQuery.getMobile()),"mobile",userInfoQuery.getMobile())
                    .eq(userInfoQuery.getStatus()!=null,"status",userInfoQuery.getStatus())
                    .eq(userInfoQuery.getUserType()!=null,"user_type",userInfoQuery.getUserType());

        IPage<UserInfo> userInfoIPage = baseMapper.selectPage(pages, queryWrapper);

        return userInfoIPage;
    }

    @Override
    public void lock(Long id, Integer status) {
        UserInfo userInfo=new UserInfo();
        userInfo.setId(id);
        userInfo.setStatus(status);
        baseMapper.updateById(userInfo);
    }

    @Override
    public Boolean checkMobile(String mobile) {
        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Integer integer = baseMapper.selectCount(queryWrapper);
        return integer>0;
    }
}
